package eoeProject.edenOfEast.page.analyses.analysesTop.component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.IAjaxCallDecorator;
import org.apache.wicket.ajax.calldecorator.AjaxCallDecorator;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;

import eoeProject.edenOfEast.bean.EOEAnalysesBean;
import eoeProject.edenOfEast.core.DBConnector;
import eoeProject.edenOfEast.core.DBUtils;
import eoeProject.edenOfEast.core.EOEAuthenticatedWebSession;
import eoeProject.edenOfEast.core.SQLMaker;
import eoeProject.edenOfEast.dataBase.DB_EOE_ANALYSES;
import eoeProject.edenOfEast.dataBase.DB_EOE_ANALYSES_CATEGORY;

//@AuthorizeInstantiation("ADMIN")
public class AnalysesTopComfirmPanel extends Panel {
	private static final long serialVersionUID = 1L;

	private Form<Void> form = new Form<Void>("form");

	private Model<String> title = new Model<String>("");

	private Model<String> category = new Model<String>("");

	private Model<String> about = new Model<String>("");

	private EOEAnalysesBean bean;

	private FeedbackPanel feedback = new FeedbackPanel("feedback");

	private Label message = new Label("message", "");

	public AnalysesTopComfirmPanel(String id, EOEAnalysesBean bean) {
		super(id);
		this.bean = bean;
		feedback.setOutputMarkupId(true);
		message.setOutputMarkupId(true);
		message.setVisible(false);

		Label titleConfirm = new Label("titleConfirm", bean.getTitle());
		form.add(titleConfirm);

		Label aboutConfirm = new Label("aboutConfirm", bean.getAbout());
		form.add(aboutConfirm);

		Label categoryConfirm = new Label("categoryConfirm",
				bean.getCategoryName());
		form.add(categoryConfirm);

		Label tag1Confirm = new Label("tag1Confirm", bean.getTag1());
		form.add(tag1Confirm);

		Label tag2Confirm = new Label("tag2Confirm", bean.getTag2());
		form.add(tag2Confirm);

		Label tag3Confirm = new Label("tag3Confirm", bean.getTag3());
		form.add(tag3Confirm);

		form.add(makeAnalysesButton);
		form.add(backButton);
		form.add(feedback);
		form.add(message);
		add(form);

	}

	private AjaxButton makeAnalysesButton = new AjaxButton("makeAnalysesButton") {
		/**
		 *
		 */
		private static final long serialVersionUID = -2942520388218435030L;

		@SuppressWarnings("rawtypes")
		@Override
		protected void onSubmit(AjaxRequestTarget target, Form form) {
			bean.setAnalysesId(DBUtils.getNextId(DB_EOE_ANALYSES.TABLE_NAME,
					DB_EOE_ANALYSES.PRIMARY_KEY));
			SQLMaker sql2 = new SQLMaker(DB_EOE_ANALYSES_CATEGORY.TABLE_NAME,
					SQLMaker.SQL_TYPE_SELECT);
			sql2.addExpEquals(DB_EOE_ANALYSES_CATEGORY.NAME,
					bean.getCategoryName());
			ResultSet rs = DBConnector.getInstance().get(sql2.getSQL());
			try {
				if (rs.next()) {
					bean.setCategoryId(rs.getInt(DB_EOE_ANALYSES.CATEGORY_ID));
				}
				rs.close();
			} catch (SQLException e) {
				// TODO 自動生成された catch ブロック
				e.printStackTrace();
			}
			bean.setCreateUserId(EOEAuthenticatedWebSession.getUserId());
			bean.setCreateUserName(EOEAuthenticatedWebSession.getUserName());
			bean.setStatus(1);
			bean.setCreateDate(new Date());
			bean.setUpdateDate(new Date());
			SQLMaker sql = new SQLMaker(DB_EOE_ANALYSES.TABLE_NAME,
					SQLMaker.SQL_TYPE_INSERT);
			sql.setInsert(bean);
			if (DBConnector.getInstance().set(sql.getSQL())) {
				setResponsePage(form.getPage());
				target.getPage()
						.get("analysesTopPanel")
						.get("analysesTopFormPanel")
						.replaceWith(
								new AnalysesTopSuccessPanel(
										"analysesTopFormPanel")
										.setOutputMarkupId(true));
			} else {
				message = new Label("message", "*登録に失敗しました。");
				message.setOutputMarkupId(true);
				message.setVisible(true);
				form.addOrReplace(message);
			}

		}

		@SuppressWarnings({ "rawtypes" })
		@Override
		protected void onError(final AjaxRequestTarget target, final Form form) {
			target.add(feedback);
		}
	};

	private AjaxButton backButton = new AjaxButton("backButton") {
		/**
		 *
		 */
		private static final long serialVersionUID = -2942520388218435030L;

		@SuppressWarnings("rawtypes")
		@Override
		protected void onSubmit(AjaxRequestTarget target, Form form) {
			setResponsePage(form.getPage());
			target.getPage()
					.get("analysesTopPanel")
					.get("analysesTopFormPanel")
					.replaceWith(
							new AnalysesTopInputPanel("analysesTopFormPanel",
									bean).setOutputMarkupId(true));
		}

		@SuppressWarnings({ "rawtypes" })
		@Override
		protected void onError(final AjaxRequestTarget target, final Form form) {
			target.add(feedback);
		}

		@Override
		protected IAjaxCallDecorator getAjaxCallDecorator() {
			return new AjaxCallDecorator() {
				/**
				 *
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public CharSequence decorateScript(Component c,
						CharSequence script) {
					return super.decorateScript(c, "savelocate(); " + script);
				}
			};
		}
	};

}
